/* 
 * @(#)ArticleUtils.java    Created on 2013-4-2
 * Copyright (c) 2013 ZDSoft Networks, Inc. All rights reserved.
 * $Id$
 */
package com.leruan.aasimp.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import com.leruan.aasimp.tabooed.BrandUtils;
import com.leruan.aasimp.tabooed.TabooedUtils;

/**
 * @author hetl
 * @version $Revision: 1.0 $, $Date: 2013-4-2 下午02:05:20 $
 */
public class ArticleUtils {
    private static final String FILE_CHARSET_DEFAULT = "utf-8";

    /**
     * 将文章内容保存成文件。
     * 
     * @param text文章内容
     * @param filePath
     *            要保存为的文件路径
     * @return true/false
     */
    public static boolean saveArticleTxtFile(String text, String filePath) {
        File file = new File(filePath);
        boolean success = true;
        BufferedReader reader = null;
        PrintWriter writer = null;

        try {
            reader = new BufferedReader(new StringReader(text));
            writer = new PrintWriter(file, FILE_CHARSET_DEFAULT);
            for (String line; (line = reader.readLine()) != null;) {
                writer.println(line);
            }
        }
        catch (IOException e) {
            success = false;
        }
        finally {
            try {
                writer.close();
            }
            catch (Exception e) {
            }

            try {
                reader.close();
            }
            catch (IOException e) {
            }
        }

        return success;
    }

    /**
     * 从文件中读取文本。
     * 
     * @param file
     *            文件对象
     * @return 文件中的文本内容
     */
    public static String readArticleFromFile(String filePath) {
        File file = new File(filePath);
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = null;
        try {
            InputStreamReader read = new InputStreamReader(new FileInputStream(file), FILE_CHARSET_DEFAULT);
            reader = new BufferedReader(read);
            for (String line; (line = reader.readLine()) != null;) {
                sb.append(line);
            }
        }
        catch (IOException e) {
        }
        finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            }
            catch (IOException e) {
            }
        }

        return sb.toString();
    }

    public static List<String> judgeTabooedWords(String sb) {

        List<String> tabooedWords = new ArrayList<String>();
        tabooedWords = TabooedUtils.getTabooedWords(sb.toString());
        return tabooedWords;
    }

    /**
     * 删除单个文件
     * 
     * @param fileName
     *            被删除文件的文件名
     * @return 单个文件删除成功返回true,否则返回false
     */
    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        if (file.isFile() && file.exists()) {
            file.delete();
            System.out.println("+++++++++++++++++++++++++++++++删除单个文件" + fileName + "成功！");
            return true;
        }
        else {
            System.out.println("++++++++++++++++++++++++++++++++删除单个文件" + fileName + "失败！");
            return false;
        }
    }

    public static void main(String[] args) {
        String text = "<Img src=\"kjkjljjl/olskdf.jpg\">";
        String filePath = "D:\\opt\\java1.txt";
        File file = new File(filePath);
        ArticleUtils.deleteFile(filePath);
    }

    public static List<String> judgeBrandWords(String sb) {
        List<String> tabooedWords = new ArrayList<String>();
        tabooedWords = BrandUtils.getTabooedWords(sb.toString());
        return tabooedWords;
    }
}
